package com.ext.aic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.ext.aic.AICManager;
import com.ext.aic.R;
import com.ext.aic.beans.HostingTimeItem;
import com.ext.aic.beans.HostingTimeItem_;
import com.ext.aic.components.PeriodOfHostingBottomPopup;
import com.ext.aic.databinding.HostingTimeItemViewBinding;
import com.ext.aic.holder.HostingTimeViewHolder;
import com.ext.aic.utils.LoadingUtils;
import com.ext.aic.utils.XPopupUtils;
import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.query.QueryBuilder;

public class HostingTimeAdapter extends RecyclerView.Adapter<HostingTimeViewHolder> {

    private List<HostingTimeItem> items = new ArrayList<>();

    public HostingTimeAdapter() {
        items.clear();
        HostingTimeItem hostingTimeItem = new HostingTimeItem();
        hostingTimeItem.setAdd(true);
        items.add(hostingTimeItem);
    }

    public void addItems(List<HostingTimeItem> items) {
        this.items.clear();
        this.items.addAll(items);
        HostingTimeItem hostingTimeItem = new HostingTimeItem();
        hostingTimeItem.setAdd(true);
        this.items.add(hostingTimeItem);
    }

    public void addItem(HostingTimeItem item) {
        if (items.isEmpty()) {
            items.add(item);
        } else if (!hasItem(item)) {
            this.items.add(items.size() - 1, item);
        }
    }

    private boolean hasItem(HostingTimeItem item) {
        for (HostingTimeItem timeItem : this.items) {
            if (timeItem.getStartHour() == item.getStartHour() && timeItem.getStartMinute() == item.getStartMinute() && timeItem.getEndHour() == item.getEndHour() && timeItem.getEndMinute() == item.getEndMinute()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public HostingTimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hosting_time_item_view, parent, false);
        return new HostingTimeViewHolder(HostingTimeItemViewBinding.bind(view));
    }

    //时间补位，不足两位前面补0
    private String timeFillPosition(int time) {
        if (time < 10) {
            return "0" + time;
        } else {
            return time + "";
        }
    }

    @Override
    public void onBindViewHolder(HostingTimeViewHolder holder, int position) {
        HostingTimeItem hostingTimeItem = items.get(position);
        if (hostingTimeItem.isAdd()) {
            holder.addTimeSlotLl.setVisibility(View.VISIBLE);
            holder.itemLl.setVisibility(View.GONE);
            holder.addTimeSlotLl.setOnClickListener(this::onItemClick);
            holder.addIv.setOnClickListener(this::onItemClick);
            holder.addTimeSlotLl.setTag(-1);
            holder.addIv.setTag(-1);
        } else {
            holder.itemLl.setVisibility(View.VISIBLE);
            holder.addTimeSlotLl.setVisibility(View.GONE);
            holder.startTimeTv.setText(String.format("%s:%s", timeFillPosition(hostingTimeItem.getStartHour()), timeFillPosition(hostingTimeItem.getStartMinute())));
            holder.endTimeTv.setText(String.format("%s:%s", timeFillPosition(hostingTimeItem.getEndHour()), timeFillPosition(hostingTimeItem.getEndMinute())));
            holder.delIv.setOnClickListener(this::onItemDelClick);
            holder.delIv.setTag(position);

            holder.itemLl.setOnClickListener(this::onItemClick);
            holder.itemLl.setTag(position);
        }
    }

    public void onItemClick(View view) {
        Context context = view.getContext();
        int position = ConvertUtils.toInt(view.getTag());
        HostingTimeItem hostingTimeItem = position >= 0 ? items.get(position) : null;
        XPopupUtils.getInstance().showBottomDialog(context, new PeriodOfHostingBottomPopup(context, hostingTimeItem));
    }

    public void onItemDelClick(View view) {
        Context context = view.getContext();
        int position = ConvertUtils.toInt(view.getTag());
        HostingTimeItem timeItem = items.get(position);
        String title = context.getString(R.string.hosting_time_del_title);
        String contentFormat = context.getString(R.string.hosting_time_del_content);
        String startTime = String.format("%s:%s", timeFillPosition(timeItem.getStartHour()), timeFillPosition(timeItem.getStartMinute()));
        String endTime = String.format("%s:%s", timeFillPosition(timeItem.getEndHour()), timeFillPosition(timeItem.getEndMinute()));
        String content = String.format(contentFormat, startTime, endTime);
        XPopupUtils.getInstance().showConfirmDialog(context, title, content, false, () -> {
            items.remove(position);
            deleteDbData(context, timeItem);
            notifyItemRemoved(position);
        });
    }

    private void deleteDbData(Context context, HostingTimeItem hostingTimeItem) {
        LoadingUtils.showLoading(context, R.string.processing);
        AICManager.getInstance().delete(HostingTimeItem.class, new Action1<QueryBuilder<HostingTimeItem>>() {
            @Override
            public void call(QueryBuilder<HostingTimeItem> builder) {
                builder.equal(HostingTimeItem_.id, hostingTimeItem.getId());
            }
        });
        LoadingUtils.hideLoading();
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
